﻿using System.Web.Mvc;
using Service.Permission;
using Utils.Types;
using Utils.Validate;
using Service.Manager;
using Service.Admin;
using System; 
using Utils.Cache;

namespace CMS.Areas.EasyUI.Controllers
{
    /// <summary>
    /// Home
    /// </summary>
    public class AdminController : BaseController
    {
        private readonly UserMenuService _usermenuservice;

        private readonly AreasService _areasservice;

        private readonly UserInfoService _userinfoservice; 

        /// <summary>
        /// AdminController
        /// </summary>
        public AdminController(UserMenuService usermenuservice, AreasService areasservice, UserInfoService userinfoservice)
        {
            _usermenuservice = usermenuservice;
            _areasservice = areasservice;
            _userinfoservice = userinfoservice;

           BaseUserInfo = _userinfoservice.GetEntityByObject<Model.Admin.UserInfo>(new { UserType = 1, UserName = "wbs" });
        }

        /// <summary>
        /// Index
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View(BaseUserInfo);
        }

        /// <summary>
        /// 错误页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Error()
        {
            return View();
        }

        /// <summary>
        /// 登录页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 注册页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Register()
        {
            return View();
        }

        /// <summary>
        /// 注册
        /// </summary> 
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult RegisterCheck(Model.Admin.UserInfo model)
        { 
            string code = Request.Form["codes"];
            string sessioncode = (string)Cache.GetCache("codes",CacheEnum.Session);
            if (code.IsNullOrEmpty() || !code.EqualsIgnore(sessioncode))
                return Json(new { code = 402, msg = "验证码错误！" });
            var userinfo = _userinfoservice.GetEntityByObject<Model.Admin.UserInfo>(new { UserType = model.UserType, UserName = model.UserName, UserEmail = model.UserEmail });
            if (userinfo != null)
                return Json(new { code = 403, msg = "账号已存在！" }); 
            
            return Json(_userinfoservice.CreateUser(model) < 1 ? new { code = 401, msg = "注册失败！" } : new { code = 200, msg = "注册成功！" });
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        public ActionResult Validate()
        {
            Tuple<string, byte[]> validate = ValidateCode.Code5();
            Cache.SetCache("codes", validate.Item1, CacheEnum.Session, 1);
            return File(validate.Item2, "image/jpeg");
        }

        /// <summary>
        /// 检测登录
        /// </summary> 
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult CheckLogin(Model.Admin.UserInfo model)
        {
            string codes = Request.Form["codes"];
            string sessioncode = (string)Cache.GetCache("codes", CacheEnum.Session);
            if (sessioncode.IsNullOrEmpty())
                return Json(new { code = 401, msg = "验证码失效，请重新获取！" });
            if (!sessioncode.EqualsIgnore(codes))
                return Json(new { code = 401, msg = "验证码错误！" });
            var userinfo = _userinfoservice.GetEntityByObject<Model.Admin.UserInfo>(new { UserType = model.UserType, UserName = model.UserName });
            if (userinfo == null)
                return Json(new { code = 401, msg = "用户不存在！" });
            if (!Utils.PassWord.MD5.Encrypt32Bit(model.PassWords).EqualsIgnore(userinfo.PassWords))
                return Json(new { code = 401, msg = "密码错误！" });
            BaseUserInfo = userinfo;
            return Json(new { code = 200, msg = "登录成功！", url = "/EasyUI/Admin/Index" });
        }

        /// <summary>
        /// 获取左侧菜单
        /// </summary> 
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult GetAddAccordion()
        { 
            return new JsonResult() { Data = _usermenuservice.GetTreeBoxList(UserID), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        /// <summary>
        /// 获取省市县镇
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAreaList(int levels = 1, int parentid = 0)
        {
            return new JsonResult { Data = _areasservice.GetList(new Model.Manager.Areas { Levels = levels, ParentID = parentid }), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    }
}